/*
 * Copyright 2009 XueSong Guo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.webwheel.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 服务器端跳转结果
 */
public class ForwardResult extends SimpleResult {

    private final String path;

    /**
     * 构造方法
     * @param path 跳转的地址
     */
    public ForwardResult(String path) {
        if(path==null) throw new IllegalArgumentException();
        this.path = path;
    }

    /**
     * 使用{@link javax.servlet.RequestDispatcher#forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)}方法跳转地址。
     * @throws Exception 任何异常
     */
    public void render() throws Exception {
        HttpServletRequest request = context.getRequest();
        HttpServletResponse response = context.getResponse();
        request.getRequestDispatcher(path).forward(request, response);
    }

    /**
     * 获得forward的目的地址
     * @return forward 地址
     */
    public String getPath() {
        return path;
    }
}
